package cn.neusoft.service.impl;


import cn.neusoft.domain.*;
import cn.neusoft.mapper.*;
import cn.neusoft.service.DisBillService;
import cn.neusoft.service.DispatchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Service
public class DispatchSImp implements DispatchService {

    @Autowired
    private WarehouseMapper warehouseMapper;
    @Autowired
    private CargoDescriptionMapper cargoDescriptionMapper;
    @Autowired
    private DeployBillMapper deployBillMapper;

    @Autowired
    private ExImportBillMapper exImportBillMapper;

    private static final int IN_Type = 1;
    private static final int OUT_Type = 0;


    @Override
    public List<String> lackCargoDesSearchAll(String warehouseName) {
        List<String> list = new ArrayList<String>();

        Warehouse warehouse = warehouseMapper.findByName(warehouseName);
        if(warehouse != null){
            for(Map.Entry<String, Double> entry : warehouse.getCargoWamLevel().entrySet()){
                if(warehouse.getCargos().containsKey(entry.getKey())){
                    if(warehouse.getCargos().get(entry.getKey()) < entry.getValue()){
                        list.add(entry.getKey());

                    }
                }
            }

        }

        return list;
    }

    @Override
    public int findCargoIsLack(String warehouseName, String cargoDesName) {
        int ans = -1;

        if(cargoDesName != null){
            Warehouse warehouse = warehouseMapper.findByName(warehouseName);
            if(warehouse != null){
                double quantities = warehouse.getCargos().get(cargoDesName);
                if(warehouse.getCargoWamLevel().get(cargoDesName) > quantities){
                    ans = 1;
                }
                else{
                    ans = 0;
                }

            }
        }

        return ans;
    }

    @Override
    public double getCarQuanInWarehouse(String warehouseName, String cargoDesName) {
        Warehouse warehouse = warehouseMapper.findByName(warehouseName);
        double quantities = -1;
        if(warehouse != null){
            for(Map.Entry<String, Double> entry : warehouse.getCargos().entrySet()){
                if(entry.getKey().equals(cargoDesName)){
                    quantities = entry.getValue();
                }
            }
        }
        return quantities;

    }

    @Override
    public int applyDepBill(int depBillID) {
        int ans = 1;

        CargoDeployBill cargoDeployBill = deployBillMapper.findByID(depBillID);
        if(cargoDeployBill != null){
            Warehouse warehouse = warehouseMapper.findByName(cargoDeployBill.getAimWarehouseName());
            Map<String, Double> quanMap = warehouse.getCargos();
            Map<String, Double> preMap = warehouse.getPreCargos();
            Map<String, Double> preOutMap = warehouse.getPreOutCargos();

            if(warehouse != null){
                switch(cargoDeployBill.getStage()){
                    case PRE_IN:
                        for(Map.Entry<String, Double> entry : cargoDeployBill.getPlanCargosInf().entrySet()){
                            Double pre = preMap.get(entry.getKey());
                            if(pre != null){
                                pre = entry.getValue() + pre;
                            }
                            else{
                                pre = entry.getValue();
                            }
                            preMap.put(entry.getKey(), pre);
                        }
                        warehouse.setPreCargos(preMap);
                        warehouseMapper.update(warehouse);
                        break;
                    case PRE_OUT:
                        for(Map.Entry<String, Double> entry : cargoDeployBill.getPlanCargosInf().entrySet()){
                            Double preOut = preOutMap.get(entry.getKey());
                            if(preOut != null){
                                preOut = preOut + entry.getValue();
                            }
                            else{
                                preOut = entry.getValue();
                            }
                            preOutMap.put(entry.getKey(), preOut);
                        }
                        ans = warehouseMapper.update(warehouse);
                        break;
                    case HAD_IN:
                        for(Map.Entry<String, Double> entry : cargoDeployBill.getRealCargosInf().entrySet()){
                            Double real = quanMap.get(entry.getKey());
                            if(real != null){
                                real = entry.getValue() + real;
                            }
                            else{
                                real = entry.getValue();
                            }
                            quanMap.put(entry.getKey(), real);
                        }
                        for(Map.Entry<String, Double> entry : cargoDeployBill.getPlanCargosInf().entrySet()){
                            Double plan = preMap.get(entry.getKey());
                            if(plan != null){
                                plan = plan - entry.getValue();
                            }
                            else{
                                plan = 0.0;
                            }
                            preMap.put(entry.getKey(), plan);
                        }
                        ans = warehouseMapper.update(warehouse);
                        if(ans > 0){
                            CargoExImportBill cargoExImportBill = new CargoExImportBill(IN_Type, cargoDeployBill.getDate(), cargoDeployBill.getRealCargosInf(),
                                    cargoDeployBill.getAimWarehouseName(), cargoDeployBill.getMissionID());
                            ans = exImportBillMapper.insert(cargoExImportBill);
                        }
                        break;
                    case HAD_OUT:
                    case RECEIVED:
                        for(Map.Entry<String, Double> entry : cargoDeployBill.getRealCargosInf().entrySet()){
                            Double real = quanMap.get(entry.getKey());
                            if(real != null){
                                real = real - entry.getValue();
                            }
                            else{
                                real = 0.0;
                            }
                            System.out.println(real + "!" + entry.getValue());
                            quanMap.put(entry.getKey(), real);
                        }
                        for(Map.Entry<String, Double> entry : cargoDeployBill.getPlanCargosInf().entrySet()){
                            Double plan = preOutMap.get(entry.getKey());
                            if(plan != null){
                                plan = plan - entry.getValue();
                            }
                            else{
                                plan = 0.0;
                            }
                            System.out.println(plan + "!" + entry.getValue());

                            preOutMap.put(entry.getKey(), plan);
                        }
                        ans = warehouseMapper.update(warehouse);
                        if(ans > 0){
                            CargoExImportBill cargoExImportBill = new CargoExImportBill(OUT_Type, cargoDeployBill.getDate(), cargoDeployBill.getRealCargosInf(),
                                    cargoDeployBill.getAimWarehouseName(), cargoDeployBill.getMissionID());
                            ans = exImportBillMapper.insert(cargoExImportBill);
                        }
                        break;
                    default:
                        break;
                }
            }
        }
        return ans;
    }
}
